<template>
  <view style="display: flex; flex-direction: row-reverse;">
    <view :class="size" class="btn-group">
      <view v-if="(item.status||'').includes('执行中')"
            class="main-btn" type="primary"
            @click.stop="() => toComplete(item)">任务完成
      </view>
      <view v-if="(item.status||'').includes('执行中')"
              class="def-btn" type="default"
              @click.stop="toBasePath('/plannedRoute/index?id=' + item.id)">选择收运
      </view>
      <view v-if="(item.status||'').includes('执行中')"
              class="def-btn"
              @click.stop="() => this.$refs.selectDialog.open(item)">
        扫码收运
      </view>
      <view v-if="!item.currentCtConfirmed && (item.status||'').includes('待接单')"
              class="main-btn" type="primary"
              @click.stop="confirmTask(item)">确认任务
      </view>
      <view v-if="(item.status||'').includes('已接单')"
              class="main-btn" type="primary"
              @click.stop="() => this.$refs.departure.open(item)">确认出车
      </view>
      <view v-if="(item.status||'').includes('执行中')"
              class="main-btn" type="primary"
              @click.stop="() => this.$refs.return.open(item)">回厂
      </view>
    </view>

    <select-dialog ref="selectDialog" @done="() => this.reload()"/>
    <departure ref="departure" @done="() => this.reload()"/>
    <return-factory ref="return" @done="() => this.reload()"/>
  </view>
</template>

<script>
import {getUserInfo, Msg, toPath} from "@/utils/BaseUtils";
import ReturnFactory from "@/pages/task/returnFactory.vue";
import SelectDialog from "@/pages/task/select-dialog.vue";
import Departure from "@/pages/task/departure.vue";
import {TaskPlanApi as Api} from "@/comms/api/taskPlanApi";

export default {
  components: {Departure, SelectDialog, ReturnFactory},
  props: {
    size: {type: String, default: 'default'},
    item: {type: Object, required: true, default: () => ({status: '',})},
  },
  emits: ['done'],
  data() {
    return {};
  },
  methods: {
    reload() {
      this.$emit("done")
    },
    toBasePath(path) {
      toPath("/pages/tasks" + path)
    },
    toComplete(data = {}) {
      toPath("/pages/tasks/plannedRoute/completeTask?id=" + data.id)
    },
    confirmTask({id}) {
      Msg.confirm("是否要确认该次任务?")
          .then(() => {
            const {userId: ctPersonId} = getUserInfo();
            Api.confirmTask({id, ctPersonId}).then(res => {
              this.reload();
              Msg.success("确认任务成功")
            })
          })
    },
  }
}
</script>

<style lang="scss" scoped>
.btn-group {
  margin-bottom: 20rpx;
  padding: 0 20rpx;
  display: flex;
  gap: 16rpx;




}

.def-btn, .main-btn {
  padding: 14rpx 26rpx !important;
  margin: 0;
  height: 100%;
  font-size: 28rpx;
  text-align: center;
  border-radius: 10rpx;
}

</style>
